<?php

namespace app\common\model\mysql;

use think\Model;

class User extends Model
{
    protected $autoWriteTimestamp = true;
    /**
     * 根据用户名获取用户信息
     * @param $username
     * @return AdminUser|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User: xyb
     * Date: 2022/11/6 21:26
     */
    public function getUserByPhoneNumber($phoneNumber)
    {
        if (empty($phoneNumber)) {
            return false;
        }
        $where = [
            'phone_number' => trim($phoneNumber),
        ];
        return $this->where($where)->find();
    }

    /**
     * @param $username
     * @return User|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User: xyb
     * Date: 2023/3/8 23:27
     */
    public function getNormalUserByUsername($username)
    {
        if (empty($username)) {
            return false;
        }
        $where = [
            'username' => trim($username),
        ];
        return $this->where($where)->find();
    }

    /**
     * 通过Id获取用户数据
     * @param $id
     * @return User|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User: xyb
     * Date: 2023/3/8 07:34
     */
    public function getUserById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }
        return $this->find($id);
    }

    /**
     * 根据用户id更新数据
     * @param $id
     * @param $data
     * @return bool
     * User: xyb
     * Date: 2022/11/6 22:25
     */
    public function updateById($id, $data)
    {
        $id = intval($id);

        if (empty($id) || empty($data) || !is_array($data)) {
            return false;
        }

        $where = [
            'id' => $id,
        ];

        return $this->where($where)->save($data);
    }
}